package com.ipay.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ipay.dao.BankInfoDao;
import com.ipay.dao.CashiersDao;
import com.ipay.dao.QRCodeDao;
import com.ipay.entity.BankInfoEntity;
import com.ipay.entity.CashiersEntity;
import com.ipay.entity.QRCodeEntity;
import com.ipay.service.CashiersService;
import com.ipay.utils.Constant;
import com.ipay.utils.DateUtils;
import com.ipay.utils.RandomStringUtils;



@Service("cashiersService")
public class CashiersServiceImpl implements CashiersService {
	@Autowired
	private CashiersDao cashiersDao;
	@Autowired
	private QRCodeDao qrCodeDao;
	@Autowired
	private BankInfoDao bankInfoDao;
	
	@Transactional
	@Override
	public void save(CashiersEntity cash) {
		cash.setAuthority(cash.getAuthority());
		cash.setRole(cash.getRole());
		cashiersDao.save(cash);
		
		String date = DateUtils.format(new Date(), "yyyyMMdd");
        String random = RandomStringUtils.randomNumeric(6);
        String codeNo = date + random;
		
        StringBuffer qrurl = new StringBuffer();
		//下载时访问地址
		qrurl.append("merchant/userAgent");//http://www.***.cn/wechat/merchant/userAgent
		qrurl.append("?merchantNo=" + cash.getMerchantId());// 商户号
        
		//新增二维码
		QRCodeEntity qRCode = new QRCodeEntity();
		qRCode.setCodeName("主码");        //收款码名称
		qRCode.setCodeNo(codeNo);       //收款码编号
		qRCode.setMerchantId(cash.getMerchantId()); //商户编号
		qRCode.setUserId(String.valueOf(cash.getId()));
		qRCode.setUrl(qrurl.toString());         //访问路径
		qRCode.setDeleteStatus(String.valueOf(Constant.DELETE_STATUS_NO));	//删除状态  0：未删除1：已删除 
		qRCode.setCodeStatus(Constant.CODE_STATUS_SD);		//二维码状态  0、系统生成 1、手动添加
		
		//根据商户号查询bankinfo表 主键id 与 默认二维码绑定
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("merchantNo", cash.getMerchantId());
		map.put("payState", Constant.Status.ENABLE.getValue());
		List<BankInfoEntity> bankList = bankInfoDao.queryList(map);
		if(bankList.size()>0){
			qRCode.setBankId(bankList.get(0).getId());//银行信息表主键id
		}else{
			qRCode.setBankId(Long.parseLong("0"));//默认值
		}
		qrCodeDao.insertQrCode(qRCode);
	}
	
	@Override
	public CashiersEntity findCashierByOpenid(String openid, int activateState) {
		return cashiersDao.findCashierByOpenid(openid,activateState);
	}

	@Override
	public List<Map<String, Object>> findCodeDataGrouping(CashiersEntity cashiersEntity) {
		return cashiersDao.findCodeDataGrouping(cashiersEntity);
	}

	@Override
	public List<CashiersEntity> queryList(Map<String, Object> map) {
		List<CashiersEntity> list = cashiersDao.queryList(map);
		if(list.size()>0){
			for(CashiersEntity cash : list){
				List<QRCodeEntity> qrList = qrCodeDao.findListByUserId(cash.getId());
				if(qrList.size() > 0){
					cash.setQrCode("true");
				}else{
					cash.setQrCode("false");
				}
			}
		}
		return list;
	}

	@Override
	public int queryTotal(Map<String, Object> map) {
		return cashiersDao.queryTotal(map);
	}

	@Override
	public CashiersEntity queryObject(Long id) {
		// TODO Auto-generated method stub
		return cashiersDao.queryObject(id);
	}

	@Override
	public void deleteBatch(Long[] ids) {
		cashiersDao.deleteBatch(ids);
	}

	@Override
	public int updateBatch(Long[] ids) {
		return cashiersDao.updateBatch(ids);
	}

	@Override
	public int update(CashiersEntity cash) {
		return cashiersDao.update(cash);
	}

	@Override
	public CashiersEntity findCashierByMobile(Map<String, Object> map) {
		return cashiersDao.findCashierByMobile(map);
	}

	@Override
	public int recoveryBatch(Long[] ids) {
		return cashiersDao.recoveryBatch(ids);
	}
}
